package com.probiz.estore.catalog.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.probiz.estore.catalog.dao.VirtualCategoryDao;
import com.probiz.estore.catalog.model.VirtualCategorySearchCriteria;
import com.probiz.estore.common.model.catalog.VirtualCategory;
import com.probiz.estore.common.util.SearchUtil;
import com.probiz.estore.core.search.SearchCriteria;
import com.probiz.estore.core.util.StringUtil;

/**
 * Dao implementation for VirtualCategory.
*/
public class VirtualCategoryDaoImpl extends BaseCategoryDaoImpl<VirtualCategory> implements VirtualCategoryDao {
   /*
    * (non-Javadoc)
    * @see com.probiz.estore.catalog.dao.VirtualCategoryDao#getCategoryByCode(java.lang.String)
    */
   public VirtualCategory getCategoryByCode(String categoryCode) {
       return (VirtualCategory) this.findUnique("from VirtualCategory c where c.categoryCode=?", categoryCode);
   }

  /*
   * (non-Javadoc)
   * @see com.probiz.estore.catalog.dao.VirtualCategoryDao#searchByCriteria(com.probiz.estore.catalog.model.VirtualCategorySearchCriteria, com.probiz.estore.core.search.SearchCriteria)
   */
   public List<VirtualCategory> searchByCriteria(VirtualCategorySearchCriteria virtualCategorySearchCriteria, SearchCriteria searchCriteria) {
       StringBuffer hqlsb = new StringBuffer(100);
       List<Object> paramList = new ArrayList<Object>();
       //start:构建HQL语句
       hqlsb.append("from VirtualCategory c where 1=1 ");
       if (StringUtils.isNotBlank(virtualCategorySearchCriteria.getKeywords())) {
           String keywords = SearchUtil.getSqlQueryValue(virtualCategorySearchCriteria.getKeywords());
           hqlsb.append(" and ");
           hqlsb.append("(c.categoryName like ? ");
           hqlsb.append(" or c.keywords like ? ");
           hqlsb.append(" or c.metaDescription like ? )");
           for (int i = 0; i < 3; i++) {
               paramList.add("%" + keywords + "%");
           }
       }

       if (StringUtils.isNotBlank(virtualCategorySearchCriteria.getCategoryName())) {
           hqlsb.append(" and c.categoryName like ?");
           paramList.add("%" + virtualCategorySearchCriteria.getCategoryName() + "%");
       }

       if (null != virtualCategorySearchCriteria.getStatus()) {
           hqlsb.append(" and c.status = ?");
           paramList.add(virtualCategorySearchCriteria.getStatus());
       }

       if (!ArrayUtils.isEmpty(virtualCategorySearchCriteria.getCategoryTypes())) {
           String categoryTypeString = StringUtil.arrayToString(virtualCategorySearchCriteria.getCategoryTypes(), ",");
           hqlsb.append(" and c.categoryType in (").append(categoryTypeString).append(")");
       }
       //end:构建HQL语句
       searchCriteria.setHql(hqlsb.toString());
       searchCriteria.removeParamValues();
       for (Object object : paramList) {

           searchCriteria.addParamValue(object);
       }
       return searchByCriteria(searchCriteria);
   }

   /*
    * (non-Javadoc)
    * @see com.probiz.estore.catalog.dao.VirtualCategoryDao#getNavigationCategories(java.lang.Integer)
    */
   public List<VirtualCategory> getNavigationCategories(Integer categoryId) {
       List<VirtualCategory> navigationCategories = new ArrayList<VirtualCategory>();
       VirtualCategory virtualCategory = this.getById(categoryId);
       if (virtualCategory == null)
           return navigationCategories;
       String categoryPath = virtualCategory.getCategoryPath();
       if (StringUtils.isNotEmpty(categoryPath)) {
           String categoryIds[] = categoryPath.split("\\.");
           for (int i = 1; i < categoryIds.length; i++) {
        	   VirtualCategory parentVirtualCategory = this
                       .getById(Integer.parseInt(categoryIds[i]));
               navigationCategories.add(parentVirtualCategory);
           }
       }
       return navigationCategories;
   }
  /*
   * (non-Javadoc)
   * @see com.probiz.estore.catalog.dao.VirtualCategoryDao#getSizeByCategoryCodeByNotCategoryId(java.lang.String, java.lang.Integer)
   */
   public Long getSizeByCategoryCodeByNotCategoryId(final String categoryCode,final Integer categoryId){
		Long size;
   	if(categoryId != null){
   		
   		size = (Long)getHibernateTemplate().executeWithNewSession(new HibernateCallback() {
   			public Object doInHibernate(Session session)
   					throws HibernateException, SQLException {
   				StringBuffer sb = new StringBuffer();
   				sb.append("select count(*) from VirtualCategory c where c.categoryCode = ? and c.categoryId != ?");
   				Query query = session.createQuery(sb.toString());
   				query.setString(0, categoryCode);
   				query.setInteger(1, categoryId);
   				Long rows =  (Long)query.uniqueResult();
   				return rows;
   			}

   		});
   	}else{
   		size = (Long)getHibernateTemplate().executeWithNewSession(new HibernateCallback() {
   			public Object doInHibernate(Session session)
   					throws HibernateException, SQLException {
   				StringBuffer sb = new StringBuffer();
   				sb.append("select count(*) from VirtualCategory c where c.categoryCode = ?");
   				Query query = session.createQuery(sb.toString());
   				query.setString(0, categoryCode);
   				Long rows =  (Long)query.uniqueResult();
   				return rows;
   			}

   		});
   	}	
   	return size;
	}
}
